package com.wang.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.web.servlet.config.annotation.*;

import java.io.File;

/**
 * @description: web配置
 * @author：tuimao
 * @date: 2024/11/11
 */
@Configurable
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    private static final long MAX_AGE = 24 * 60 * 60; //一天

    @Value("${file.upload.path}")
    private String uploadDir;

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("*")
//                .allowedHeaders("*")
//                .maxAge(MAX_AGE);
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor) //登录拦截器
                .addPathPatterns("/**")
                .excludePathPatterns("/user/doLogin");
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        // 假设图片上传到了项目根目录下的 upload 文件夹
//        registry.addResourceHandler("/file/**")
//                .addResourceLocations("file:" + uploadDir);
//        super.addResourceHandlers(registry);
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/images/**").addResourceLocations("file:" + uploadDir);
//        registry.addResourceHandler("/static/**")
//                .addResourceLocations("classpath:/static/");
//        registry.addResourceHandler("/mapper/**")
//                .addResourceLocations("classpath:/mapper/");
//        // 所有路径下都可以访问
//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/static/");
        // 允许swagger 访问静态资源
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
